class AnalizeController < ApplicationController
  before_action :set_menu

  def part_salse
    @submenu = :part_salse
    @title = '配件销售统计'
    @start = Time.now.beginning_of_month.strftime("%Y-%m-%d")
    @end = Time.now.strftime("%Y-%m-%d")
    @part_salse_details = PartSalseDetail.where(created_at: @start..@end)
    if params.has_key? :check
      @part_salse_details = PartSalseDetail.where(created_at: Time.parse(params[:start])..Time.parse(params[:end])+1.day)
    end
  end

  def stockin_stockout
    @submenu = :stockin_stockout
    @title = '出入库明细统计'
    @start = Time.now.beginning_of_month.strftime("%Y-%m-%d")
    @end = Time.now.strftime("%Y-%m-%d")
    @part_stockin_details = PartStockinDetail.where(created_at: @start..@end)
    @part_stockout_details = PartStockoutDetail.where(created_at: @start..@end)
    if params.has_key? :check
      @part_stockin_details = PartStockinDetail.where(created_at: Time.parse(params[:start])..Time.parse(params[:end])+1.day)
      @part_stockout_details = PartStockoutDetail.where(created_at: Time.parse(params[:start])..Time.parse(params[:end])+1.day)
    end
  end

  def business_income
    @submenu = :business_income
    @title = '营业收入明细'
    @start = Time.now.beginning_of_month.strftime("%Y-%m-%d")
    @end = Time.now.strftime("%Y-%m-%d")
    @order_maintains = OrderMaintain.where(pay_datetime: @start..@end)
    @order_beauties = OrderBeauty.where(pay_datetime: @start..@end)
    if params.has_key? :check
      @order_maintains = OrderMaintain.where(pay_datetime: Time.parse(params[:start])..Time.parse(params[:end])+1.day)
      @order_beauties = OrderBeauty.where(pay_datetime: Time.parse(params[:start])..Time.parse(params[:end])+1.day)
    end
  end

  def maintain_income
    @submenu = :maintain_income
    @title = '维修收入日明细'
    @start = Time.now.strftime("%Y-%m-%d")
    @order_maintains = OrderMaintain.where(pay_datetime: Time.now-1.day..@start)
    if params.has_key? :check
      @order_maintains = OrderMaintain.where(pay_datetime: Time.parse(params[:start])..Time.parse(params[:start])+1.day)
    end
    @order_maintain_total = 0
    @order_maintains.each {|x| @order_maintain_total += x.total}
    @order_maintain_cost = 0
    @order_maintains.each {|x| @order_maintain_cost += x.cost}
  end

  def staff_achievement
  	@submenu = :staff_achievement
    @title = '员工绩效统计'
  	@staffs = Staff.all
    @chart = LazyHighCharts::HighChart.new('graph') do |f|
      f.title(text: "员工工时")
      f.xAxis(categories: @staffs.map{|s| s.name})
      f.series(name: "美容工时", data: @staffs.map{|s| s.cal_beauty_hour})
      f.series(name: "维修工时", data: @staffs.map{|s| s.cal_maintain_hour})

      f.yAxis [
        {title: {text: "工时", margin: 70} }
      ]

      f.legend(align: 'right', verticalAlign: 'top', y: 75, x: -50, layout: 'vertical')
      f.chart({defaultSeriesType: "column"})
    end

    @chart_globals = LazyHighCharts::HighChartGlobals.new do |f|
      f.global(useUTC: false)
      f.chart(
        backgroundColor: {
          linearGradient: [0, 0, 500, 500],
          stops: [
            [0, "rgb(255, 255, 255)"],
            [1, "rgb(240, 240, 255)"]
          ]
        },
        borderWidth: 2,
        plotBackgroundColor: "rgba(255, 255, 255, .9)",
        plotShadow: true,
        plotBorderWidth: 1
      )
      f.lang(thousandsSep: ",")
      f.colors(["#90ed7d", "#f7a35c", "#8085e9", "#f15c80", "#e4d354"])
    end
  end

  def business_total
    @submenu = :business_total
    @title = '营业综合统计'
    @start = Time.now.beginning_of_month.strftime("%Y-%m-%d")
    @end = Time.now.strftime("%Y-%m-%d")
    @part_purchases = PartPurchase.where(pay_datetime: @start..@end)
    @part_salses = PartSalse.where(pay_datetime: @start..@end)
    @order_maintains = OrderMaintain.where(pay_datetime: @start..@end)
    @order_beauties = OrderBeauty.where(pay_datetime: @start..@end)
    if params.has_key? :check
      @part_purchases = PartPurchase.where(pay_datetime: Time.parse(params[:start])..Time.parse(params[:end])+1.day)
      @part_salses = PartSalse.where(pay_datetime: Time.parse(params[:start])..Time.parse(params[:end])+1.day)
      @order_maintains = OrderMaintain.where(pay_datetime: Time.parse(params[:start])..Time.parse(params[:end])+1.day)
      @order_beauties = OrderBeauty.where(pay_datetime: Time.parse(params[:start])..Time.parse(params[:end])+1.day)

    end
    @part_purchase_total = 0
    @part_purchases.each {|x| @part_purchase_total += x.total}
    @part_salse_total = 0
    @part_salses.each {|x| @part_salse_total += x.total}
    @order_maintain_total = 0
    @order_maintains.each {|x| @order_maintain_total += x.total}
    @order_beauty_total = 0
    @order_beauties.each {|x| @order_beauty_total += x.total}
    @order_maintain_cost = 0
    @order_maintains.each {|x| @order_maintain_cost += x.cost}
    @order_beauty_cost = 0
    @order_beauties.each {|x| @order_beauty_cost += x.cost}
  

  end


  private
  def set_menu
    @mainmenu = :analize
  end
end
